﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using SaludOcupacional.Exceptions;
using SaludOcupacional.Model;
using System.Collections.ObjectModel;

namespace SaludOcupacional.DataAccess
{
    public class AudiometricoDataAccess : IDisposable
    {
        private DatabaseManager dbManager;

        public void Dispose()
        {
            dbManager.Dispose();
        }
        public AudiometricoDataAccess()
        {
            dbManager = new DatabaseManager();
        }

        public Audiometrico BuscarAnalisisPorCi(int ci)
        {
            if (ci == 0)
            {
                throw new ArgumentNullException("ci");
            }
            // Create a Sql sentence
            string sqlsentence = "SELECT * FROM [AnalisisAudiometrico] WHERE ci_empleado = @Ci";

            dbManager.Command.CommandText = sqlsentence;
            dbManager.Command.Parameters.AddWithValue("@Ci", ci);

            Audiometrico resp = null;

            try
            {
                DataSet dataSet = dbManager.Query();

                if (dataSet.Tables[0].Rows.Count > 0)
                {
                    DataRow reader = dataSet.Tables[0].Rows[0];

                    resp = new Audiometrico
                    {
                        CodAnalisis = Convert.ToInt32(reader["cod_analisis"], System.Globalization.CultureInfo.InvariantCulture),
                        CiEmpleado = Convert.ToInt32(reader["ci_empleado"], System.Globalization.CultureInfo.InvariantCulture),
                        Izq = reader["oido_izq"].ToString(),
                        Der = reader["oido_der"].ToString(),
                        Bilateral = reader["bilateral"].ToString(),
                        Acciones = reader["acciones_atomar"].ToString(),
                        Observaciones = reader["observaciones"].ToString(),
                        Fecha = reader["fecha"].ToString()

                    };
                }
            }
            catch (Exception)
            {
                throw;
            }

            return resp;
        }

        public void RegistrarAnalisis(Audiometrico audiometrico)
        {
            if (audiometrico == null)
            {
                throw new LoginException("Invalid Input Parameters");
            }

            string sqlSentence = "INSERT INTO [dbo].[AnalisisAudiometrico]([ci_empleado],[oido_izq],[oido_der],[bilateral],[acciones_atomar],[observaciones],[fecha])VALUES ( ";
            sqlSentence += "@Ci, @izq, @der, @Bilateral, @accion, @observ, @fec )";

            dbManager.Command.CommandText = sqlSentence;
            dbManager.Command.CommandType = System.Data.CommandType.Text;



            dbManager.Command.Parameters.AddWithValue("@Ci", audiometrico.CiEmpleado);
            dbManager.Command.Parameters.AddWithValue("@izq", audiometrico.Izq);
            dbManager.Command.Parameters.AddWithValue("@der", audiometrico.Der);
            dbManager.Command.Parameters.AddWithValue("@Bilateral", audiometrico.Bilateral);
            dbManager.Command.Parameters.AddWithValue("@accion", audiometrico.Acciones);
            dbManager.Command.Parameters.AddWithValue("@observ", audiometrico.Observaciones);
            dbManager.Command.Parameters.AddWithValue("@fec", audiometrico.Fecha);


            try
            {
                int value = dbManager.NonQuery();

                if (value < 1)
                {
                    throw new LoginException("The record wasn't registered");
                }
            }
            catch (Exception e)
            {
                throw new LoginException("Error creating a user" + e.Message, e);
            }
        }

        public void EditarAnalisis(Audiometrico audiometrico)
        {
            if (audiometrico == null)
            {
                throw new LoginException("The Analisis can´t be null");
            }

            string sqlSentence =
                "UPDATE [AnalisisAudiometrico]  SET [oido_izq] = @izq, [oido_der] = @der, [bilateral] = @Bilateral, [acciones_atomar] = @accion, ";
            sqlSentence = sqlSentence + "[observaciones] = @observ, [fecha] = @fec ";
            sqlSentence = sqlSentence + " WHERE [ci_empleado] = @ci";

            dbManager.Command.CommandText = sqlSentence;
            dbManager.Command.CommandType = System.Data.CommandType.Text;


            dbManager.Command.Parameters.AddWithValue("@izq", audiometrico.Izq);
            dbManager.Command.Parameters.AddWithValue("@der", audiometrico.Der);
            dbManager.Command.Parameters.AddWithValue("@Bilateral", audiometrico.Bilateral);
            dbManager.Command.Parameters.AddWithValue("@accion", audiometrico.Acciones);
            dbManager.Command.Parameters.AddWithValue("@observ", audiometrico.Observaciones);
            dbManager.Command.Parameters.AddWithValue("@fec", audiometrico.Fecha);
            dbManager.Command.Parameters.AddWithValue("@ci", audiometrico.CiEmpleado);


            try
            {
                int value = dbManager.NonQuery();
                if (value < 1)
                {
                    throw new LoginException("The change wasn't registered");
                }

            }
            catch (Exception e)
            {
                throw new LoginException("Error edit profile", e);
            }
        }
    }
}
